package org.jeecg.api.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.api.annotation.ApiNeedLogin;
import org.jeecg.api.dto.req.user.WorkResumeDto;
import org.jeecg.api.utils.MiniappSpringContextUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.recruit.appuser.entity.MmAppUserWorkResume;
import org.jeecg.modules.recruit.appuser.service.IMmAppUserWorkResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信小程序用户接口
 */
@RestController
@RequestMapping("/app/work/")
@Api(tags = "工作简历接口")
@Slf4j
@ApiNeedLogin
public class WxMiniappWorkResumeController {
    @Autowired
    private IMmAppUserWorkResumeService mmAppUserWorkResumeService;

    @PostMapping("resume/add")
    @ApiOperation(value = "添加工作简历")
    public Result<Boolean> addWorkResume(@RequestBody WorkResumeDto workResumeDto) {
        MmAppUserWorkResume resume = new MmAppUserWorkResume();
        resume.setUserId(MiniappSpringContextUtils.getSignDO().getUserId());
        resume.setCompanyName(workResumeDto.getCompanyName());
        resume.setEntryDate(workResumeDto.getEntryDate());
        resume.setLeaveDate(workResumeDto.getLeaveDate());
        resume.setMonthPay(new BigDecimal(String.valueOf(workResumeDto.getMonthPay())));
        mmAppUserWorkResumeService.save(resume);
        return Result.OK();
    }

    @GetMapping("resume/get")
    @ApiOperation("获取工作简历")
    public Result<List<WorkResumeDto>> getWorkResume() {
        String userId = MiniappSpringContextUtils.getSignDO().getUserId();
        List<MmAppUserWorkResume> list = mmAppUserWorkResumeService.lambdaQuery().
                eq(MmAppUserWorkResume::getUserId, userId).list();
        List<WorkResumeDto> collect = list.stream().map(resume -> {
            WorkResumeDto workResumeDto = new WorkResumeDto();
            workResumeDto.setCompanyName(resume.getCompanyName());
            workResumeDto.setEntryDate(resume.getEntryDate());
            workResumeDto.setLeaveDate(resume.getLeaveDate());
            workResumeDto.setMonthPay(resume.getMonthPay().doubleValue());
            return workResumeDto;
        }).collect(Collectors.toList());
        return Result.OK(collect);
    }

}